package com.bdth.weget.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.weget.common.MessageResult;
import com.bdth.weget.common.Pagination;
import com.bdth.weget.entity.CarCare;
import com.bdth.weget.entity.User;
import com.bdth.weget.service.CarCareService;
import com.bdth.weget.service.CarsService;
import com.bdth.weget.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 维修保养记录 前端控制器
 * </p>
 *
 * @author LGP
 * @since 2019-07-10
 */
@Controller
@RequestMapping("/carcare")
public class CarCareController extends BaseController {
    @Autowired
    private CarCareService carCareService;

    @Autowired
    private CarsService carsService;

    @RequestMapping(value = "index")
    public String index() {
        return "carcare/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<CarCare> getPageList(HttpServletRequest request, Pagination<CarCare> pagination,
                                       @RequestParam(required = false) String plateno){
        Map<String, Object> query = new HashMap<>();
        User session = (User)request.getSession().getAttribute("loginUser");
        if(session.getGroupid()!=0) {
            String carids = carsService.getUserCars(session.getDepartids());
            query.put("carids", carids);
        }
        query.put("plateno",plateno);
        Page<CarCare> items = carCareService.listPages(pagination,query);
        PageVo<CarCare> pageVo = new PageVo<>(items.getRecords(),items.getTotal());
        return pageVo;
    }


    @RequestMapping(value = "form")
    public String form(){
        return "carcare/form";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(CarCare param){
        boolean flag = carCareService.save(param);
        if(flag)
            return MessageResult.getSuccess("添加数据成功");
        else
            return MessageResult.getError("500","添加数据失败");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(CarCare param){
        boolean flag = carCareService.update(param);
        if(flag)
            return MessageResult.getSuccess("修改数据成功");
        else
            return MessageResult.getError("500","修改数据失败");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        boolean flag = carCareService.delete(ids);
        if(flag)
            return MessageResult.getSuccess("删除数据成功");
        else
            return MessageResult.getError("500","删除数据失败");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        boolean flag = carCareService.remove(ids);
        if(flag)
            return MessageResult.getSuccess("删除数据成功");
        else
            return MessageResult.getError("500","删除数据失败");
    }

    @RequestMapping(value = "search")
    public String search(){
        return "carcare/search";
    }

    @RequestMapping(value = "edit")
    public String edit(){
        return "carcare/edit";
    }
}

